package p2pcdnsim.network;

public final class GenericAddress implements NetworkAddress {
	
	private String routerAddress;
	private String clientAddress;
	
	public GenericAddress(String routerAddress, String clientAddress) {
		this.routerAddress = routerAddress;
		this.clientAddress = clientAddress;
	}

	public GenericAddress(String routerAddress) {
		this.routerAddress = routerAddress;
		this.clientAddress = null;
	}
	
	public GenericAddress(GenericAddress serverAddress, String clientAddress) {
		this(serverAddress.routerAddress,clientAddress);
	}

	public NetworkAddress getRouterAddress() {
		return new GenericAddress(routerAddress);
	}

	@Override
	public boolean isClientAddress() {
		return clientAddress != null;
	}
	
	@Override
	public boolean equals(Object object) {
		if(!(object instanceof GenericAddress)) return false;
		GenericAddress obj = (GenericAddress) object;
		if(routerAddress.equals(obj.routerAddress)) {
			if(clientAddress != null && obj.clientAddress != null)
				return clientAddress.equals(obj.clientAddress);
			if(clientAddress == null && obj.clientAddress == null)
				return true;
			return false;
		} else return false;
	}

	public String toString() {
		return routerAddress+ (clientAddress == null ? "" : "."+clientAddress);
	}

	@Override
	public int hashCode() {
		return routerAddress.hashCode() + (clientAddress == null ? 0 : clientAddress.hashCode());
	}

}
